package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author 董代恒
 * @version 1.0
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setMealDishMapper;


    @Override
    public void saveDishWithFlavors(DishDTO dishDTO) {
        log.debug("新增菜品:disDTO:{}", dishDTO);
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //向菜品表插入一条数据
        dishMapper.insertDish(dish);
        //获取insert语句生成的主键id
        Long id = dish.getId();

        //插入口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                //给插入的每一个口味都设置该菜品id
                dishFlavor.setDishId(id);
            });
            //向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult selectPage(DishPageQueryDTO dishPageQueryDTO) {
        //1.开始分页
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());


        Page<DishVO> page = dishMapper.select(dishPageQueryDTO);

        long total = page.getTotal();
        List result = page.getResult();

        return new PageResult(total, result);
    }

    @Override
    public void delete(List<Long> ids) {
        //业务1：可以一次删除一个菜品，也可以批量删除菜品

        //业务2：起售中的菜品不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.selectStatusById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //业务3:被套餐关联的菜品不能删除
        List<Long> setmealByDishId = setMealDishMapper.getSetmealByDishId(ids);
        if (setmealByDishId != null && setmealByDishId.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //业务4:删除菜品后，关联的口味数据也要删除掉
        //这里循环删除菜品在sql中就不用循环遍历了
        for (Long id : ids) {
            //先删除菜品

            dishMapper.delete(id);

            //再删除口味
            dishFlavorMapper.deleteFlavorByDishId(id);
        }
    }

    @Override
    public List<Dish> selectDishByCategoryId(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE).build();
        List<Dish> dishList = dishMapper.selectDishByCategoryId(dish);
        return dishList;
    }
}
